package com.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.beans.FlowerInfo;
import com.service.FlowerService;

@Controller  @RequestMapping("/flower")
public class FlowerController {
	@Resource
	private FlowerService flowerService;
	
	//用于国际化
	@Resource 
	private MessageSource messageSource;
		
	//全局的,用于转向某个页面
	@GetMapping("/{page}")
	public String toPage(@PathVariable("page") String page) {
		return page;   //默认会转向 src/main/resources/templates/+page+.html
	}
	 
	//添加图书
	@PostMapping("/add")
	public String add(FlowerInfo flower,ModelMap m)
	{
		flowerService.add_flower(flower);
		m.put("msg","鲜花添加成功");
		return "flower-add";
	}
	
	@GetMapping("/getAll")
	public String getAll(ModelMap m) {
		List<FlowerInfo> flowerList= flowerService.select_all();
		m.put("flowerList",flowerList);
		m.put("size",flowerList.size());
		return "flower-manager";	
	}
	
	@GetMapping("/getAll1")
	public String getAll1(ModelMap m) {
		List<FlowerInfo> flowerList= flowerService.select_all();
		m.put("flowerList",flowerList);
		m.put("size",flowerList.size());
		return "flower-manager-user";	
	}
/*	
	@RequestMapping("/pageMy")
	public String pageMy(FlowerInfo flowerInfo,Integer pageNum,Integer pageSize,Model model){
	  //        pageSize = 3;
	  //一页的数据集合
	  PageHelper.startPage(pageNum, pageSize);
	  List<FlowerInfo> flowerInfos = flowerService.queryByPage(flowerInfo);

	  //分页的page数据,比如：几页，每页几条数据pageNum=1, pageSize=5, startRow=0, endRow=5, total=6, pages=2
	  //此处用的绝对名称，因为自己建的实体类与此类重名，com.github.pagehelper.PageInfo
	  com.github.pagehelper.PageInfo<FlowerInfo> flowerList = new com.github.pagehelper.PageInfo<FlowerInfo>(flowerInfos);

	  model.addAttribute("flowerList",flowerInfos);
	  model.addAttribute("flowerInfo",flowerList);
	  return "flower-manager";
	}
*/	
	@GetMapping("/update")
	public String searchForUpdate(int id,ModelMap m) {
		FlowerInfo flower=flowerService.get_flower(id);
		m.put("flower", flower);
		return "flower-update"; 
	}
	
	@PostMapping("/update")
	public String update(FlowerInfo flower,ModelMap m) {
		m.put("flower",flower);
		flowerService.update_flower(flower);
		m.put("msg","鲜花信息更新成功");
		return "flower-update"; 
	}
	
	@GetMapping("/delete")
	public String delete(int id) {
		flowerService.del_flower(id);
		return "forward:/flower/getAll";	
	}
	
	  @RequestMapping("/allFlower")
	  public String getFlowerList(@RequestParam(value = "flowerName",required = false)String flowerName,Model model) {
	      System.out.println(flowerName);
//	      判空操作，如果判断没有输入信息，就模糊查询所有数据
	      if (flowerName==null){
	          List<FlowerInfo> flowerList = flowerService.flowerList("%");
	          model.addAttribute("flowerList",flowerList);
	      }
	      else {
//	      如果有输入的数据，那么将使用正规的模糊查询
	          List<FlowerInfo> flowerList = flowerService.flowerList("%"+flowerName+"%");
	          model.addAttribute("flowerList",flowerList);
	      }
	      return "flower-manager";
	}
	  
	  @RequestMapping("/allFlower1")
	  public String getFlowerList1(@RequestParam(value = "flowerName",required = false)String flowerName,Model model) {
	      System.out.println(flowerName);
//	      判空操作，如果判断没有输入信息，就模糊查询所有数据
	      if (flowerName==null){
	          List<FlowerInfo> flowerList = flowerService.flowerList("%");
	          model.addAttribute("flowerList",flowerList);
	      }
	      else {
//	      如果有输入的数据，那么将使用正规的模糊查询
	          List<FlowerInfo> flowerList = flowerService.flowerList("%"+flowerName+"%");
	          model.addAttribute("flowerList",flowerList);
	      }
	      return "flower-manager-user";
	}
}
